package com.wyt.controller.util;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wyt.pojo.Book;
import com.wyt.pojo.NewUser;
import com.wyt.pojo.User;
import com.wyt.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
public class UserController {
    @Autowired
    private UserService userService;
    @GetMapping("/admin/user")
    public String user(){
        return "redirect:/admin/user.html";
    }

    @GetMapping("/getUserJson")
    @ResponseBody
    public Data getUserJson(Integer page, Integer limit,@RequestParam(required = false) String searchParams){
        Data data = new Data();
        Map map = (Map) JSON.parse(searchParams);
        List<User> userList =null;
        if (map!=null){
            String ID = (String)map.get("ID");
            String name = (String)map.get("name");
            String publisher = (String)map.get("publisher");
            userList = userService.selectByPage(page,limit,ID,name,publisher);
        }else {
            userList = userService.selectByPage(page,limit,null,null,null);
        }
        long count = userService.count();
        List<NewUser> newUserList = new ArrayList<>();
        //实现limit转换
        for(User user :userList){
            NewUser newUser = new NewUser(user.getId(), user.getName(), user.getLimit(), user.getPassword(), user.getAge(), user.getBook1Id(), user.getBook2Id());
            newUserList.add(newUser);
        }
        data.setData(newUserList);
        data.setCode("0");
        data.setCount(count+"");
        return data;
    }

    @GetMapping("/add_user")
    public String add_user(){
        return "redirect:/admin/add_user.html";
    }

    @GetMapping("/edit_user")
    public String edit_user(){
        return "redirect:/admin/edit_user.html";
    }

    @PostMapping("/index/admin/addUserJson")
    @ResponseBody
    public Data addUserJson(User user){
        Data data = new Data();
        int save = userService.addUser(user.getId(),user.getName(),user.getLimit(),"123456");
        if (save==1){
            data.setCode("1");
            data.setMessage("添加成功");
        }else {
            data.setCode("0");
            data.setMessage("添加失败");
        }
        return data;
    }
    @PostMapping("/index/admin/deleteUserJson")
    @ResponseBody
    public Data deleteUserJson(User user){
        Data data = new Data();
        boolean b = userService.removeById(user);
        if (b){
            data.setCode("1");
            data.setMessage("删除成功");
        }else {
            data.setCode("0");
            data.setMessage("删除失败");
        }
        return data;
    }

    @PostMapping("/index/admin/deleteAllUserJson")
    @ResponseBody
    public Data deleteAllUserJson(@RequestParam Map userMap){
        Data data = new Data();
        ArrayList<String> list = new ArrayList<>();
        int i=0;
        for (Object key : userMap.keySet()){
            if (i%7==0){
                list.add(String.valueOf(userMap.get(key)));
            }
            i++;
        }
        boolean b = userService.removeBatchByIds(list);
        if (b){
            data.setCode("1");
            data.setMessage("删除成功");
        }else {
            data.setCode("0");
            data.setMessage("删除失败");
        }
        return data;
    }
//    private static List<User> updateUser(List<User> userList){
//        for (User user :userList) {
//            if (user.getLimit()==1){
//                user.
//            }
//        }
//        return ;
//    }
}
